function handling() {
    $("#login-loading").show();
}
function errorNotice(msg) {
    alert(msg);
}

//设置时间
var countdown=59;
function settime() {
    if (countdown == 0) {
        $('#getCode').attr("disabled",false);
        $('#getCode').val("获取验证码");
        countdown = 59;
        return;
    } else {
        $('#getCode').attr("disabled", 'disabled');
        $('#getCode').val("重新发送(" + countdown + ")");
        countdown--;
    }
    setTimeout(function() {
            settime();
        }
    ,1000);

}

//获取验证码
$("#getCode").click(function () {
    var phonenum = $("#phone").val();
    if (phonenum.length == 11 && phonenum >= 13000000000 && phonenum <= 19999999999) {
        $("#login-error").hide();
    } else {
        errorNotice('手机号格式错误');
        return false;
    }

    if(!$("#csessionid").val() || !$("#csessionid").val() || !$("#csessionid").val() || !$("#csessionid").val()) {
        errorNotice('请先通过滑动验证');
        return false;
    } else {
        $("#login-error").hide();
    }
    handling();
    $.ajaxPost(apiHost+"api/sms/send",{
        'mobile': phonenum,
        'csessionid':$("#csessionid").val(),
        'sig':$("#sig").val(),
        'token':$("#token").val(),
        'scene':$("#scene").val(),
        'checkType':'slide',
        "type":1,
        "event":"mobilelogin"
    }, function (data) {
        $("#login-loading").hide();
        if (data.code == 1) {
            settime();
        } else{
            errorNotice(data.msg);
            showCaptcha('captcha');
        }
    });
});

//提交登录
$("#fm-login-submit").click(function() {
    var phonenum = $("#phone").val();
    var code = $("#fm-login-password").val();

    if (phonenum.length == 11 && phonenum >= 13000000000 && phonenum <= 19999999999) {
        $("#login-error").hide();
    } else {
        errorNotice('手机号格式错误');
        return false;
    }

    if(!$("#csessionid").val() || !$("#csessionid").val() || !$("#csessionid").val() || !$("#csessionid").val()) {
        errorNotice('请先通过滑动验证');
        return false;
    } else {
        $("#login-error").hide();
    }

    if (code.length != 4) {
        errorNotice('验证码错误');
        return false;
    } else {
        $("#login-error").hide();
    }
    handling();
    $.ajaxPost(apiHost+"api/user/mobilelogin", {
        'mobile': phonenum,
        "captcha": code,
        "client":0,
        "location_href":window.location.href
    }, function(data) {
        $("#login-loading").hide();
        if(data.code) {
            window.sessionStorage.setItem('token',data.data['userinfo']['token']);
            location.href = data.data['returnUrl'];
        } else {
            errorNotice(data.msg);
        }
    });
});

function showCaptcha(boxid) {
    var nc_appid = "FFFF0N000000000064CC";
    var nc_scene = "nc_login";
    var nc_token = [nc_appid, (new Date()).getTime(), Math.random()].join(':');
    var nc= new noCaptcha({
        renderTo: '#' + boxid,
        appkey: nc_appid, 
        scene: nc_scene,
        token: nc_token,
        initHidden:false,
        callback: function (data) {
            document.getElementById('csessionid').value = data.csessionid;
            document.getElementById('sig').value = data.sig;
            document.getElementById('token').value = nc_token;
            document.getElementById('scene').value = nc_scene;
        },
        error: function (s) {
            console.log(s);
        }
    });
}

$(".phone-login").click(function() {
    $(".phone-login-box").fadeIn("fast");
    $(".btn-login-box").hide();

    showCaptcha("captcha");
});

$(".back-login").click(function() {
    $(".phone-login-box").hide();
    $(".btn-login-box").fadeIn("fast");
});